<?
function get_searchresults(){
	global $tree, $root_path, $http_path;

	$exclude=array("or", "and", "if", "a", "an", "the", "you", "your", "at", "i");
	$exclude_rus=array("и", "не", "для", "на", "под", "в", "от", "об", "о");
	$garbage=array(". ", ", ", ": ", "; ", ")", "(", "?", "*", ".");

	$_GET["keywords"]=str_replace($garbage, "", $_GET["keywords"]);

	if (trim($_GET["keywords"])=="") {
		return array("0"=>array("error_report"=>"Nothing found. Sorry..."));
	}
	$keyarr=explode(" ", trim(str_replace('"', '', stripslashes($_GET["keywords"]))));

	foreach ($keyarr as $key=>$word) {
		if(in_array(strtolower($word), $exclude) or in_array(strtolower($word), $exclude_rus)) unset($keyarr[$key]);
	}
	$keywords=implode(" ", $keyarr);

	$GLOBALS["env"]["keywords"]=$keywords;
	$keywords1=preg_replace("/\s+/i", "\s+", addcslashes(trim($keywords), "/"));
	$keywords2=preg_replace("/\s+/i", ".*", addcslashes(trim($keywords), "/"));
	$db = new sapi_vdb();
	foreach ($tree->map as $id=>$document) {
		if ($id!=0) {
			$data=$db->get_data($root_path . "usr/xml/content/" . $id . ".xml");
			if(is_array($data[1]))
			foreach ($data[1] as $key=>$value) {
				if($key=="DATE_CREATE") $date=$value;
				if($key!="IP" and $key!="DATE_CREATE"){
					$value=strip_tags($value);
					$value=str_replace($garbage, " ", $value);
					if(preg_match("/\s+".$keywords1."\s+/isU", $value, $matches)) {
						$match="...".substr($value, strpos($value, $matches[0])-100, strpos($value, $matches[0])+100)."...";
						foreach ($keyarr as $word) $match=preg_replace("/(".$word.")/is", "<b>\\1</b>", $match);
						$tmp[$id]["id"]=$id;
						$tmp[$id]["DATE_CREATE"]=$date;
						$tmp[$id]["matches"][]=$match;
						//		$tmp[$id]["cnt1"]++;
						if($tmp[$id]["rel"]==0) $tmp[$id]["rel"]=10;
						foreach ($keyarr as $word){
							$drel=(strlen($value)/strlen($matches[0]))*substr_count(strtoupper($value),strtoupper($word))/(10*count($keyarr));
							if($drel>100) $drel=$drel/10;
							$tmp[$id]["rel"]+=$drel;
						}
					}elseif(preg_match("/\s+".$keywords2."\s+/isU", $value, $matches)) {
						$match="...".$matches[0]."...";
						foreach ($keyarr as $word) $match=preg_replace("/(".$word.")/is", "<b>\\1</b>", $match);
						$tmp[$id]["id"]=$id;
						$tmp[$id]["DATE_CREATE"]=$date;
						$tmp[$id]["matches"][]=$match;
						//		$tmp[$id]["cnt2"]++;
						if($tmp[$id]["rel"]==0) $tmp[$id]["rel"]=5;
						foreach ($keyarr as $word){
							$drel=(strlen($value)/strlen($matches[0]))*substr_count(strtoupper($value),strtoupper($word))/(10*count($keyarr));
							if($drel>100) $drel=$drel/10;
							$tmp[$id]["rel"]+=$drel;
						}

					}
				}
			}
		}
	}
	if(is_array($tmp)){
		usort($tmp, "cmp");
		$maxrelev=$tmp[0]["rel"];
		$cnt=0;
		//d($tmp);
		foreach ($tmp as $key=>$value) {

			if(search_perm_check($tree->map[$value["id"]])){
				$cnt++;
				$stream[$key]["TITLE"]=$tree->map[$value["id"]]["TITLE"];
				$stream[$key]["HREF"]=preg_replace("/\/$/", "", $http_path) . $tree->map[$value["id"]]["HREF"];
				foreach (array_unique($value["matches"]) as $text) $stream[$key]["MATCHES"].=$text."<br />";
				//$stream[$key]["RELEVANCY"]=$value["cnt1"]*3+$value["cnt2"];
				$stream[$key]["RELEVANCY"]=(int)(100*$value["rel"]/$maxrelev);
				$stream[$key]["RELEVANCY"]=(int)($value["rel"]);
				$stream[$key]["COUNTER"]=$cnt;
				$stream[$key]["DATE_CREATE"]=$value["DATE_CREATE"];
			}

		}

		if($stream) {
			$__counter = 0;
			$sapi_obj->env["this.lenght"] = count($stream);
			foreach ($stream as $key => $fetch) {
				$stream[$key]["counter"] = ++$__counter;
			}
		}


		if(is_array($stream)) return $stream; else return array("0"=>array("error_report"=>"Nothing found. Sorry..."));
	}else return array("0"=>array("error_report"=>"Nothing found. Sorry..."));

}

function cmp($a, $b){
	//	if(($a["cnt1"]*3+$a["cnt2"])==($b["cnt1"]*3+$b["cnt2"])) return 0;
	//	return (($a["cnt1"]*3+$a["cnt2"])>($b["cnt1"]*3+$b["cnt2"]))?-1:1;
	if($a["rel"]==$b["rel"]) return 0;
	return ($a["rel"]>$b["rel"])?-1:1;
}

function search_perm_check($page){
	if($page["PERMISSIONS"]!=""){
		if($_SESSION["user"]["GROUP"]==$page["GROUP"]){
			$pCurrent=substr($page["PERMISSIONS"], 1, 1);
		}else{
			$pCurrent=substr($page["PERMISSIONS"], 2, 1);
		}
		if($pCurrent>4) return true;
	}
	return false;
}
?>